// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <optional>

#include <pollux/testing/functions/prestosql/function_base_test.h>
#include <pollux/functions/sparksql/tests/ArraySortTestData.h>
#include <pollux/functions/sparksql/tests/SparkFunctionBaseTest.h>
#include <pollux/vector/complex_vector.h>

namespace kumo::pollux::functions::sparksql::test {
namespace {

using namespace kumo::pollux::test;

using kumo::pollux::functions::test::FunctionBaseTest;

class ArraySortTest : public SparkFunctionBaseTest {
 protected:
  void testArraySort(const VectorPtr& input, const VectorPtr& expected) {
    auto result = evaluate("array_sort(c0)", make_row_vector({input}));
    assertEqualVectors(expected, result);
  }

  template <typename T>
  void testInt() {
    auto input = make_nullable_array_vector(intInput<T>());
    auto expected = make_nullable_array_vector(intAscNullLargest<T>());
    testArraySort(input, expected);
  }

  template <typename T>
  void testFloatingPoint() {
    auto input = make_nullable_array_vector(floatingPointInput<T>());
    auto expected = make_nullable_array_vector(floatingPointAscNullLargest<T>());
    testArraySort(input, expected);
  }
};

TEST_F(ArraySortTest, int8) {
  testInt<int8_t>();
}

TEST_F(ArraySortTest, int16) {
  testInt<int16_t>();
}

TEST_F(ArraySortTest, int32) {
  testInt<int32_t>();
}

TEST_F(ArraySortTest, int64) {
  testInt<int64_t>();
}

TEST_F(ArraySortTest, float) {
  testFloatingPoint<float>();
}

TEST_F(ArraySortTest, double) {
  testFloatingPoint<double>();
}

TEST_F(ArraySortTest, string) {
  auto input = make_nullable_array_vector(stringInput());
  auto expected = make_nullable_array_vector(stringAscNullLargest());
  testArraySort(input, expected);
}

TEST_F(ArraySortTest, timestamp) {
  auto input = make_nullable_array_vector(timestampInput());
  auto expected = make_nullable_array_vector(timestampAscNullLargest());
  testArraySort(input, expected);
}

TEST_F(ArraySortTest, date) {
  auto input = make_nullable_array_vector(dateInput(), ARRAY(DATE()));
  auto expected = make_nullable_array_vector(dateAscNullLargest(), ARRAY(DATE()));
  testArraySort(input, expected);
}

TEST_F(ArraySortTest, bool) {
  auto input = make_nullable_array_vector(boolInput());
  auto expected = make_nullable_array_vector(boolAscNullLargest());
  testArraySort(input, expected);
}

TEST_F(ArraySortTest, array) {
  auto input = make_nullable_nested_array_vector(arrayInput());
  auto expected = make_nullable_nested_array_vector(arrayAscNullLargest());
  testArraySort(input, expected);
}

TEST_F(ArraySortTest, map) {
  auto input = make_array_of_map_vector(mapInput());
  auto expected = make_array_of_map_vector(mapAscNullLargest());
  testArraySort(input, expected);
}

TEST_F(ArraySortTest, row) {
  auto rowType = ROW({INTEGER(), VARCHAR()});
  auto input = make_array_of_row_vector(rowType, rowInput());
  auto expected = make_array_of_row_vector(rowType, rowAscNullLargest());
  testArraySort(input, expected);
}

TEST_F(ArraySortTest, constant) {
  vector_size_t size = 1'000;
  auto data =
      make_array_vector<int64_t>({{1, 2, 3, 0}, {4, 5, 4, 5}, {6, 6, 6, 6}});

  auto evaluateConstant = [&](vector_size_t row, const VectorPtr& vector) {
    return evaluate(
        "array_sort(c0)",
        make_row_vector({BaseVector::wrap_in_constant(size, row, vector)}));
  };

  auto result = evaluateConstant(0, data);
  auto expected = make_constant_array<int64_t>(size, {0, 1, 2, 3});
  assertEqualVectors(expected, result);

  result = evaluateConstant(1, data);
  expected = make_constant_array<int64_t>(size, {4, 4, 5, 5});
  assertEqualVectors(expected, result);

  result = evaluateConstant(2, data);
  expected = make_constant_array<int64_t>(size, {6, 6, 6, 6});
  assertEqualVectors(expected, result);
}
} // namespace
} // namespace kumo::pollux::functions::sparksql::test
